package CutLoad.components.container
{
	import CutLoad.Base;
	import CutLoad.CutLoad;
	import CutLoad.components.CScrollBar;
	import CutLoad.cookies.Painter;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;

	/**
	 * 含有水平滚动条的容器 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CScrollH extends Base
	{
		private var body:Base
		private var scrollBar:CScrollBar;
		private var displayWidth:Number,displayHeight:Number;
		private var bodyMask:Base;
		private var tagetX:Number=0;
		//
		private var isChangBody:Boolean;
		//
		private var offsetSpeed:Number;
		/**
		 * 
		 * @param body 要滚动的内容
		 * @param w 容器的宽 
		 * @param h 容器的高
		 * @param offsetSpeed 内容滚动的缓动参数
		 * @param isShowUpDownBtn 是否显示上下按钮
		 * @param numSeed 上下移动的速度
		 * @param isChangBody 当改变垂直滚动条容器宽高时，默认会同时改变装在它里面内容（body）的宽高,默认为true;
		 * 
		 * 
		 * 注：当改变垂直滚动条容器宽高时，默认会同时改变装在它里面内容（body）的宽高,如果不想改变请使用CScrollV.resizeBodyWH=true/false取消
		 * 
		 */		
		public function CScrollH(body:Base,w:Number,h:Number,offsetSpeed:Number=0.3,isShowUpDownBtn:Boolean=true,numSeed:Number=2,isChangBody:Boolean=true)
		{
			this.isChangBody=isChangBody;
			this.offsetSpeed=offsetSpeed;
			scrollBar=new CScrollBar();
			scrollBar.init(numSeed,scrollCheck,isShowUpDownBtn);
			scrollBar.rotation+=90;
			scrollBar.x+=w;
			bodyMask=Painter.drawRect(w,h,0x000000,0.3);
			body.show(this);
			//
			scrollBar.visible=false;
			scrollBar.show(this);
			//
			displayWidth=w;
			displayHeight=h;
			resetBody(body);
			bodyMask.show(this);
			bodyMask.mouseChildren=false;
			bodyMask.mouseEnabled=false;
			//
			this.addEventListener(Event.ENTER_FRAME,enterFrame);
			//this.addEventListener(FocusEvent.FOCUS_IN,focusIn);
			//this.addEventListener(FocusEvent.FOCUS_OUT,focusOut);
		}
		/**
		 * 获得滚动的内容 
		 * @return 
		 * 
		 */		
		public function getBody():Base{
			return body;
		}
		/**
		 * 重新设置滚动条容器的内容 
		 * @param body
		 * 
		 */		
		public function resetBody(body:Base):void{
			if(this.body!=null) this.removeChild(this.body);
			this.body=body;
			body.show(this);
			scrollBar.visible=false;
			scrollBar.show(this);
			body.mask=bodyMask;
			setWH(this.getShowWidth(),this.getShowHeight())
			
		}
		private function focusIn(e:FocusEvent):void{
			this.addEventListener(MouseEvent.MOUSE_WHEEL,wheel);
		}
		private function focusOut(e:FocusEvent):void{
			this.removeEventListener(MouseEvent.MOUSE_WHEEL,wheel);
		}
		private function wheel(e:MouseEvent):void{
			//trace(e.delta)
		}
		//滚动值	
		private function scrollCheck(value:Number):void{
			if(!scrollBar.visible) return;
			//如果有滚动条，时刻检测滚动情况
			var temH:Number=body.getShowWidth()-this.getShowWidth();
			tagetX=-temH*value;
		}
		private function enterFrame(e:Event):void{
			body.x+=(tagetX-body.x)*offsetSpeed;
		}
		/**
		 * 当改变垂直滚动条容器宽高时，是否要同时改变装在它里面内容（body）的宽高. 
		 * @param isChangBody
		 * 
		 */		
		public function set resizeBodyWH(isChangBody:Boolean):void{
			this.isChangBody=isChangBody;
		}
		public function get resizeBodyWH():Boolean{
			return this.isChangBody
		}
		/**
		 * 设置内容坐标 ,注：yy指的是离body注册点的高度
		 * @param yy
		 * @param hhForCenter 默认为0,如果要设置相对滚动显示内容高度，居中的话。可以设置以yy为起点，的高度
		 * 
		 */		
		public function setBodyY(yy:Number,hhForCenter:Number=0):void{
			var temH:Number=body.getShowHeight()-this.getShowHeight();
			if(hhForCenter!=0){
				hhForCenter=(this.getShowHeight()-hhForCenter)/2;
				hhForCenter=yy-hhForCenter
				if(hhForCenter<0) hhForCenter=0;
				scrollBar.value=(hhForCenter/temH);
			}else{
				scrollBar.value=((yy)/temH);
			}
			
			
		}
		/**
		 * 滚动条向上或向下 
		 * @param t 格式：只能是:1向上,2向下
		 * @param MultipleSpeed 速度的倍数移动
		 * @param isAddY 是否在滚动y基础上相加。
		 * 
		 */		
		public function moveBar(t:Number=1,MultipleSpeed:Number=1,isAddY:Boolean=true):void{
			scrollBar.moveBar(t,MultipleSpeed,isAddY);
		}
		/**
		 * 是否有按住滚动条 
		 * @return 
		 * 
		 */		
		public function getIsDownBar():Boolean{
			return scrollBar.getIsDownBar();
		}
		/**
		 * 滚动条移动底部 
		 * 
		 */
		public function moveBarEnd():void{
			scrollBar.value=1;
			scrollCheck(1);
		}
		/**
		 * 刷新 
		 * 
		 */		
		public function refresh():void{
			this.setWH();
		}
		override public function setWH(w:Number=0, h:Number=0):void{
			if(w==0) w=this.getShowWidth();
			if(h==0) h=this.getShowHeight();
			this.displayWidth=w;
			this.displayHeight=h;
			//重新设置大小
			var bodyW:Number=w;
			var bodyH:Number;
			//是否显示滚动条
			var bodyHaveWidth:Number=body.getShowWidth()
			if(bodyW<bodyHaveWidth){
				//如果显示滚动条的话，需要修改body的宽度
				bodyW=w-scrollBar.getShowWidth()
				if(isChangBody) body.setWH(body.getShowWidth(),bodyH);
				//
				scrollBar.visible=true;
				scrollBar.y=this.getShowHeight()-scrollBar.getShowHeight();
				scrollBar.setWH(0,w);
			}else{
				scrollBar.visible=false
				bodyW=w;
				if(isChangBody) body.setWH(body.getShowWidth(),bodyH);
			}
			tagetX=0;
			body.x=0;
			//scrollBar.value=0;
			//设置遮蔽宽高
			//bodyMask.setWH(bodyW,body.getShowHeight())
		}
		/**
		 * 返回滚动条的引用 
		 * @return 
		 * 
		 */		
		public function getScroll():CScrollBar{
			return scrollBar;
		}
		override public function getShowWidth():Number{
			return displayWidth;
		}
		override public function getShowHeight():Number{
			return displayHeight;
		}
	}
}